﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "EditFrameInstanceTypeParametersCommand.h"
#include "IDocument.h"
#include "UniIdentity.h"
#include "GDFoundation/Command.h"
#include "GDDrawing/IDrawingFrame.h"
#include "GDDrawing/IDrawingFrameStyle.h"
#include "UiDocumentViewUtils.h"
#include "ISelection.h"
#include "IHighlights.h"
#include "IGraphicsNodeReference.h"
#include "IGenericElement.h"
#include "IUiView.h"
#include "IExternalObject.h"
#include "CommandParameters.h"
#include "IFamily.h"
#include "IInstanceType.h"
#include "ICommandManager.h"

using namespace gcmp;
using namespace gdmp;
namespace Sample
{
   
    bool EditFrameInstanceTypeParametersCommand::IsEnabled() const
    {
        return IsVisible();
    }

    bool EditFrameInstanceTypeParametersCommand::IsVisible() const
    {
        gcmp::IDocument* pDocument = GetDocument();
        if (!pDocument)
            return false;

        return true;
    }

    gcmp::OwnerPtr<gcmp::IAction> EditFrameInstanceTypeParametersCommand::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
    {
        gcmp::IDocument* pDocument = GetDocument();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDocument, L"断言`pDocument`失败",L"GDMPLab",L"2024-03-30");

        ISelection* pSelection = ISelection::Get();
        if (pSelection->GetCount()<=0)
        {
            return nullptr;
        }
        const IGraphicsNodeReference* pGraphicsNodeReference = pSelection->GetGraphicsNodeReferences().begin()->get();
        ElementId eleId = pGraphicsNodeReference->GetElementId();

        IElement* pElement = pDocument->GetElement(eleId);
        IGenericElement* pGenElement = quick_cast<IGenericElement>(pElement);
        if (!pGenElement)
            return nullptr;

        IDrawingFrame* pDrawingFrame = quick_cast<IDrawingFrame>(pGenElement->GetExternalObject());
        if (!pDrawingFrame)
            return nullptr;

        const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pDocument->GetElement(pDrawingFrame->GetDrawingFrameStyleId()));
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"设置的图框样式pGenericElement无效",L"GDMPLab",L"2024-03-30");
        const IDrawingFrameStyle* pFrameStyle = quick_cast<IDrawingFrameStyle>(pGenericElement->GetExternalObject());
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pFrameStyle, L"设置的图框样式pFrameStyle无效",L"GDMPLab",L"2024-03-30");
        // 此处作为示例，只显示标题栏族类型的参数。
        // 用户可以用同样方式获取会签栏（DrawingFrameFamilyType::CountersignFamily）或页签栏（DrawingFrameFamilyType::PageFamily）族类型的参数。
        std::wstring familyName = pFrameStyle->GetFrameFamily(DrawingFrameFamilyType::TitleFamily);
        gcmp::IFamily* pFamily = IFamily::GetFamilyByName(pDocument, familyName);
        if (!pFamily)
        {
            return nullptr;
        }
        std::vector<IInstanceType*> allTypes = pFamily->GetInstanceTypes();
        const IInstanceType* pType = nullptr;
        if (allTypes.size() > 0)
        {
            pType = allTypes[0];
        }
        DBG_WARN_AND_RETURN_FALSE_UNLESS(pType, L"pType为空",L"GDMPLab",L"2024-03-30");
        const ElementId typeId = pType->GetElementId();
        // 此处为了解耦模块间依赖关系，采用了发送命令的方式打开类型参数界面，命令ID为"ShowTypeParametersCmd"，
        // 对应的命令响应代码为CmdTypeProperty.cpp文件中的CmdTypeProperty。
        gcmp::CommandParameters commandParams;
        commandParams.emplace(L"ParamTypeId", typeId.AsInt64());
        gcmp::ICommandManager* pCommandManager = gcmp::ICommandManager::Get();
        DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pCommandManager, L"pCommandManager为空",L"GDMPLab",L"2024-03-30");
        pCommandManager->SendCommand(L"ShowTypeParametersCmd", commandParams);

        return nullptr;
    }

    gcmp::IDocument* EditFrameInstanceTypeParametersCommand::GetDocument() const
    {
        gcmp::IDocument* pDocument = UiDocumentViewUtils::GetCurrentDocument();
        if (pDocument) {
            if (!pDocument->IsFamilyDocument()) {
                return pDocument;
            }
        }
        return nullptr;
    }

    namespace
    {
        //仅作为示例展示使用，注册命令,当析构时移除注册
        gdmp::Command stub{ NEW_AS_OWNER_PTR(EditFrameInstanceTypeParametersCommand) };
    }
}
